import torch
import numpy as np
import matplotlib.pyplot as plt

Fs = 200 # Sampling frequency
T = 1 / Fs # period
t = 1 # time
L = Fs * t # signle length

x = np.arange(L)*T
y = 20*np.sin(2*np.pi*2*x) + 38*np.sin(2*np.pi*13*x)

plt.figure(figsize=(9,6), dpi=200)
plt.figure(1)
ax1=plt.subplot(311)
ax1.plot(x,y)
ax1.set_xlabel('Time (s)')
ax1.set_ylabel('Amplitude')

# plt.xlabel('Time (s)')
# plt.ylabel('Amplitude')
# plt.show()

p = torch.fft.fft(torch.from_numpy(y))
p = 2 * torch.abs(p) / len(p)
p = p[:int(len(p) / 2)]
p[0] = 0.
freq_bin = np.arange(int(L/2))*Fs/L
ax2=plt.subplot(313)
ax2.plot(freq_bin,p)
ax2.set_xlabel('Frequency (Hz)')
ax2.set_ylabel('|P1|')

# plt.plot(freq_bin,p)
# plt.xlabel('Frequency (Hz)')
# plt.ylabel('|P1|')
plt.show()
